import pylab as pl
import matplotlib
import utils as ut
import methods
import tkinter as tk
from tkinter import ttk

window = tk.Tk()  # 创建窗口
window.title("软件缺陷预测")  # 窗口标题
window.geometry('600x480')  # 窗口大小，小写字母x

var = tk.IntVar()   # 实验
dtr = tk.IntVar()   # 数据集
fstr = tk.IntVar()  # 特征选择
mtr = tk.IntVar()   # 算法
str = tk.IntVar()   # 指标
ntr1 = tk.IntVar()  # 同构
ntr2 = tk.IntVar()  # 异构

global experiment
experiment = "0"
global dataset_name
dataset_name = "Ant"
global score_name
score_name = "AUC"
global fs_functions
fs_functions = ['pearson', 'fisher', 'greedy']
global method
method = ['RF', 'W_SVM', 'APE']
global n_classifiers
n_classifiers = 1

# ########实验选择########
k1 = tk.Label(window,
             text='选择要做的实验',  # 文本
             font=(12),  # 字体和大小
             width=30, height=2  # 字体所占的宽度和高度
             )
# k1.grid(row=0, column=1, sticky=tk.E)
k1.place(x=35, y=15)

def experimentChoice():
    global experiment
    if var.get() == 0:
        experiment = "0"
        k5.place_forget()
        k6.place_forget()
        m0.place_forget()
        m1.place_forget()
        m2.place_forget()
        fs0.place_forget()
        fs1.place_forget()
        fs2.place_forget()
        scale1.place_forget()
        scale2.place_forget()
        k4.place(x=355, y=225)
        fsm0.place(x=385, y=270)
        fsm1.place(x=485, y=270)
        fsm2.place(x=385, y=300)
        fsMulChoice()

    elif var.get() == 1:
        experiment = "1"
        k6.place_forget()
        fsm0.place_forget()
        fsm1.place_forget()
        fsm2.place_forget()
        scale1.place_forget()
        scale2.place_forget()
        k4.place(x=355, y=225)
        fs0.place(x=385, y=270)
        fs1.place(x=485, y=270)
        fs2.place(x=385, y=300)
        k5.place(x=355, y=330)
        m0.place(x=385, y=375)
        m1.place(x=485, y=375)
        m2.place(x=385, y=405)
        fsChoice()
        mMulChoice()

    elif var.get() == 2:
        experiment = "2"
        k5.place_forget()
        k4.place_forget()
        m0.place_forget()
        m1.place_forget()
        m2.place_forget()
        fs0.place_forget()
        fs1.place_forget()
        fs2.place_forget()
        fsm0.place_forget()
        fsm1.place_forget()
        fsm2.place_forget()
        scale2.place_forget()
        k6.place(x=355, y=255)
        scale1.place(x=415, y=290)

    elif var.get() == 3:
        experiment = "3"
        k5.place_forget()
        k4.place_forget()
        m0.place_forget()
        m1.place_forget()
        m2.place_forget()
        fs0.place_forget()
        fs1.place_forget()
        fs2.place_forget()
        fsm0.place_forget()
        fsm1.place_forget()
        fsm2.place_forget()
        scale1.place_forget()
        k6.place(x=355, y=255)
        scale2.place(x=415, y=290)

    elif var.get() == 4:
        experiment = "4"
        k4.place_forget()
        k5.place_forget()
        k6.place_forget()
        m0.place_forget()
        m1.place_forget()
        m2.place_forget()
        fs0.place_forget()
        fs1.place_forget()
        fs2.place_forget()
        fsm0.place_forget()
        fsm1.place_forget()
        fsm2.place_forget()
        scale1.place_forget()
        scale2.place_forget()

e0 = tk.Radiobutton(window, text='实验一 对比不同特征选择算法对APE的积极影响',
                    variable=var,
                    value=0,
                    command=experimentChoice)
# e0.grid(row=1, column=1, sticky=tk.W)
e0.place(x=35, y=70)

e1 = tk.Radiobutton(window, text='实验二 对比APE与其他机器学习算法的性能',
                    variable=var,
                    value=1,
                    command=experimentChoice)
# e1.grid(row=2, column=1, sticky=tk.W)
e1.place(x=35, y=110)

e2 = tk.Radiobutton(window, text='实验三 探究同构基分类器数量对APE的性能的影响',
                    variable=var,
                    value=2,
                    command=experimentChoice)
# e2.grid(row=3, column=1, sticky=tk.W)
e2.place(x=35, y=150)

e3 = tk.Radiobutton(window, text='实验四 探究异构基分类器数量对APE的性能的影响',
                    variable=var,
                    value=3,
                    command=experimentChoice)
# e3.grid(row=4, column=1, sticky=tk.W)
e3.place(x=35, y=190)

e4 = tk.Radiobutton(window, text='实验五 探究不同基分类器组合对APE的性能的影响',
                    variable=var,
                    value=4,
                    command=experimentChoice)
# e4.grid(row=5, column=1, sticky=tk.W)
e4.place(x=35, y=230)


# ########数据集选择########
k2 = tk.Label(window,
             text='选择数据集',  # 文本
             font=(12),  # 字体和大小
             width=25, height=2  # 字体所占的宽度和高度
             )
# k2.grid(row=0, column=2, columnspan=2, sticky=tk.W+tk.E)
k2.place(x=355, y=15)

def datasetsChoice():
    global dataset_name
    if dtr.get() == 0:
        dataset_name = "Ant"
    elif dtr.get() == 1:
        dataset_name = "Camel"
    elif dtr.get() == 2:
        dataset_name = "KC3"
    elif dtr.get() == 3:
        dataset_name = "MC1"
    elif dtr.get() == 4:
        dataset_name = "PC2"
    elif dtr.get() == 5:
        dataset_name = "PC4"
    else:return

data0 = tk.Radiobutton(window, text="Ant",
                       variable=dtr,
                       value=0,
                       command=datasetsChoice)
# data0.grid(row=1, column=2, sticky=tk.W+tk.E)
data0.place(x=385, y=60)

data1 = tk.Radiobutton(window, text="Camel",
                       variable=dtr,
                       value=1,
                       command=datasetsChoice)
# data1.grid(row=1, column=3, sticky=tk.W+tk.E)
data1.place(x=485, y=60)

data2 = tk.Radiobutton(window, text="KC3",
                       variable=dtr,
                       value=2,
                       command=datasetsChoice)
# data2.grid(row=2, column=2, sticky=tk.W+tk.E)
data2.place(x=385, y=90)

data3 = tk.Radiobutton(window, text="MC1",
                       variable=dtr,
                       value=3,
                       command=datasetsChoice)
# data3.grid(row=2, column=3, sticky=tk.W+tk.E)
data3.place(x=485, y=90)

data4 = tk.Radiobutton(window, text="PC2",
                       variable=dtr,
                       value=4,
                       command=datasetsChoice)
# data4.grid(row=3, column=2, sticky=tk.W+tk.E)
data4.place(x=385, y=120)

data5 = tk.Radiobutton(window, text="PC4",
                       variable=dtr,
                       value=5,
                       command=datasetsChoice)
# data5.grid(row=3, column=3, sticky=tk.W+tk.E)
data5.place(x=485, y=120)

# ########指标选择########
k3 = tk.Label(window,
             text='评估指标',  # 文本
             font=(12),  # 字体和大小
             width=25, height=2  # 字体所占的宽度和高度
             )
# k3.grid(row=4, column=2, columnspan=2, sticky=tk.W+tk.E)
k3.place(x=355, y=150)

def score_nameChoice():
    global score_name
    if str.get() == 0:
        score_name = "AUC"
    elif str.get() == 1:
        score_name = "GMeans"
    else:return

s0 = tk.Radiobutton(window, text="AUC",
                       variable=str,
                       value=0,
                       command=score_nameChoice)
# s0.grid(row=5, column=2, sticky=tk.W+tk.E)
s0.place(x=385, y=195)


s1 = tk.Radiobutton(window, text="G-Mean",
                       variable=str,
                       value=1,
                       command=score_nameChoice)
# s1.grid(row=5, column=3, sticky=tk.W+tk.E)
s1.place(x=485, y=195)


# ########特征选择算法########
k4 = tk.Label(window,
             text='特征选择算法',  # 文本
             font=(12),  # 字体和大小
             width=25, height=2  # 字体所占的宽度和高度
             )
# k4.grid(row=6, column=2, columnspan=2, sticky=tk.W+tk.E)
k4.place(x=355, y=225)

# ########单选########
def fsChoice():
    global fs_functions
    if fstr.get() == 0:
        fs_functions = ["pearson"]
    elif fstr.get() == 1:
        fs_functions = ["fisher"]
    elif fstr.get() == 2:
        fs_functions = ["greedy"]
    else:return

fs0 = tk.Radiobutton(window, text="pearson",
                       variable=fstr,
                       value=0,
                       command=fsChoice)
# fs0.grid(row=7, column=2, sticky=tk.W+tk.E)
fs0.place(x=385, y=270)

fs1 = tk.Radiobutton(window, text="fisher",
                       variable=fstr,
                       value=1,
                       command=fsChoice)
# fs1.grid(row=7, column=3, sticky=tk.W+tk.E)
fs1.place(x=485, y=270)

fs2 = tk.Radiobutton(window, text="greedy",
                       variable=fstr,
                       value=2,
                       command=fsChoice)
# fs2.grid(row=8, column=2, sticky=tk.W+tk.E)
fs2.place(x=385, y=300)

fsVar1 = tk.IntVar()
fsVar2 = tk.IntVar()
fsVar3 = tk.IntVar()
# ########多选########
def fsMulChoice():
    tmp_fs_function = []
    if fsVar1.get() == 0:
        tmp_fs_function.append("pearson")
    if fsVar2.get() == 0:
        tmp_fs_function.append("fisher")
    if fsVar3.get() == 0:
        tmp_fs_function.append("greedy")

    global fs_functions
    fs_functions = tmp_fs_function

fsm0 = tk.Checkbutton(window, text="pearson",
                       variable=fsVar1,
                       onvalue=0,
                      offvalue=1,
                       command=fsMulChoice)
# fsm0.grid(row=7, column=2, sticky=tk.W+tk.E)
fsm0.place(x=385, y=270)

fsm1 = tk.Checkbutton(window, text="fisher",
                       variable=fsVar2,
                      onvalue=0,
                      offvalue=1,
                       command=fsMulChoice)
# fsm1.grid(row=7, column=3, sticky=tk.W+tk.E)
fsm1.place(x=485, y=270)

fsm2 = tk.Checkbutton(window, text="greedy",
                       variable=fsVar3,
                      onvalue=0,
                      offvalue=1,
                       command=fsMulChoice)
# fsm2.grid(row=8, column=2, sticky=tk.W+tk.E)
fsm2.place(x=385, y=300)

# ########机器学习算法########
k5 = tk.Label(window,
             text='机器学习算法',  # 文本
             font=(12),  # 字体和大小
             width=25, height=2  # 字体所占的宽度和高度
             )
# k5.grid(row=9, column=2, columnspan=2, sticky=tk.W+tk.E)
# k5.grid_remove()
k5.place(x=355, y=330)
k5.place_forget()

mVar1 = tk.IntVar()
mVar2 = tk.IntVar()
mVar3 = tk.IntVar()
def mMulChoice():
    tmp_method = []
    if mVar1.get() == 0:
        tmp_method.append("RF")
    if mVar2.get() == 0:
        tmp_method.append("W_SVM")
    if mVar3.get() == 0:
        tmp_method.append("APE")
    global method
    method = tmp_method

m0 = tk.Checkbutton(window, text="RF",
                       variable=mVar1,
                       onvalue=0,
                      offvalue=1,
                       command=mMulChoice)
# m0.grid(row=10, column=2, sticky=tk.W+tk.E)
# m0.grid_remove
m0.place(x=385, y=375)
m0.place_forget()

m1 = tk.Checkbutton(window, text="W_SVM",
                       variable=mVar2,
                      onvalue=0,
                      offvalue=1,
                       command=mMulChoice)
# m1.grid(row=10, column=3, sticky=tk.W+tk.E)
# m1.grid_remove()
m1.place(x=485, y=375)
m1.place_forget()

m2 = tk.Checkbutton(window, text="APE",
                       variable=mVar3,
                      onvalue=0,
                      offvalue=1,
                       command=mMulChoice)
# m2.grid(row=11, column=2, sticky=tk.W+tk.E)
# m2.grid_remove()
m2.place(x=485, y=375)
m2.place_forget()

# ########基分类器数量########
k6 = tk.Label(window,
             text='基分类器数量',  # 文本
             font=(12),  # 字体和大小
             width=25, height=2  # 字体所占的宽度和高度
             )
# k6.grid(row=6, column=2, columnspan=2, sticky=tk.W+tk.E)
# k6.grid_remove()
k6.place(x=355, y=225)
k6.place_forget()

# ########同构基分类器数量########
scale1 = tk.Scale(window, variable=ntr1, from_=1, to=25, orient=tk.HORIZONTAL)
# scale1.grid(row=7, column=2, columnspan=2, sticky=tk.W+tk.E)
# scale1.grid_remove()
scale1.place(x=375, y=270)
scale1.place_forget()

# ########异构基分类器数量########
scale2 = tk.Scale(window, variable=ntr2, from_=1, to=11, orient=tk.HORIZONTAL)
# scale2.grid(row=7, column=2, columnspan=2, sticky=tk.W+tk.E)
# scale2.grid_remove()
scale2.place(x=375, y=380)
scale2.place_forget()

def start():

    global n_classifiers
    if experiment == "2":
        n_classifiers = ntr1.get()
    elif experiment == "3":
        n_classifiers = ntr2.get()

    # 设置字体和画布大小
    matplotlib.rc("font", family='MicroSoft YaHei')
    pl.figure(figsize=(6, 4), dpi=240)
    # 打印参数信息
    print("\nDATASET: %s\nMETHOD: %s\nEXPERIMENT: %s\n" % (dataset_name, method, experiment))
    print(fs_functions)
    # 读取数据，所有的特征存储到X中，有/无缺陷的结果存储到y中
    X, y = ut.read_dataset("datasets/", dataset_name=dataset_name)
    # 设置绘图的标题和xy轴
    if experiment == "2" or experiment == "3":
        pl.xlabel("基分类器数", loc="right")
    elif experiment == "4":
        pl.xlabel("基分类器组合类型", loc="right")
    else:
        pl.xlabel("保留特征数", loc="right")
    pl.ylabel(score_name, loc="top")
    if experiment == "0":
        pl.title("特征选择算法对比\n" + dataset_name)
    elif experiment == "1":
        pl.title("缺陷预测算法对比\n" + dataset_name + " " + fs_functions[0])
    elif experiment == "2":
        pl.title("基分类器数量对同构APE性能的影响")
    elif experiment == "3":
        pl.title("基分类器数量对异构APE性能的影响\n" + dataset_name)
    elif experiment == "4":
        pl.title("基分类器组合对异构APE性能的影响\n" + dataset_name)

    methods.run_method(method, X, y, n_classifiers=n_classifiers, fs_functions=fs_functions,
                       score_name=score_name, experiment=experiment)

    pl.legend(loc="best")
    pl.show()

btnOK = tk.Button(window, text="开始", command=start, width=20)
# btnOK.grid(row=9, column=1,sticky=tk.W+tk.E)
btnOK.place(x=105, y=320)

separatorLine = ttk.Separator(window, orient='vertical')
separatorLine.place(x=355, y=0, height=550)

# 以上是窗口的主体
window.mainloop()  # 结束（不停循环刷新）

